using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Runtime.Serialization;

namespace TemplateWeb.DAO
{
	#region using
	using System.Data;
	using TemplateWeb.DTO;
	using TemplateWeb.Helpers;
	#endregion

	/// <summary>
	/// Data access object, which get data of SanPham table from database
	/// </summary>
	public class SanPhamDAO : AbstractDAO
	{
		public SanPhamDAO(string strPath): base(strPath)
		{
		}

		public DataSet SelectAll()
		{
			DataSet ds;
			try
			{
				ds = new DataSet();
				base.sql = "usp_SanPham_SelectAll";
				base.FillDataSet(ref ds, "SanPham");
				return ds;
			}
			catch(System.Exception ex)
			{
				if (ex.InnerException != null)
				{
					CommonLogging.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					CommonLogging.Logger.Error(ex.Message);
				}
			}
			finally
			{
				base.CleanUpObject();
			}
			return null;
		}

		public DataSet SelectByID(int id)
		{
			DataSet ds;
			try
			{
				ds = new DataSet();
				base.sql = "usp_SanPham_SelectByID";
				base.InitializeCommand();
				base.AddParameter("@ID", SqlDbType.Int, 1000, id);
				base.FillDataSet(ref ds, "SanPham");
				return ds;
			}
			catch(System.Exception ex)
			{
				if (ex.InnerException != null)
				{
					CommonLogging.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					CommonLogging.Logger.Error(ex.Message);
				}
			}
			finally
			{
				base.CleanUpObject();
			}
			return null;
		}

		public bool Insert(SanPhamDTO obj)
		{
			try
			{
				base.sql = "usp_SanPham_Insert";
				base.InitializeCommand();
				base.AddParameter("@IDDanhMucCon", SqlDbType.Int, 1000, obj.IDDanhMucCon);
				base.AddParameter("@IDToChucCaNhan", SqlDbType.Int, 1000, obj.IDToChucCaNhan);
				base.AddParameter("@TenSanPham", SqlDbType.NVarChar, 1000, obj.TenSanPham);
				base.AddParameter("@XuatXu", SqlDbType.NVarChar, 1000, obj.XuatXu);
				base.AddParameter("@MoTa", SqlDbType.NVarChar, 1000, obj.MoTa);
				base.AddParameter("@HinhAnh", SqlDbType.NVarChar, 1000, obj.HinhAnh);
				base.AddParameter("@SoLuongTon", SqlDbType.Int, 1000, obj.SoLuongTon);

                base.AddParameterWithDirection("@ID", SqlDbType.Int, 1000);

                var kq = ExecuteStoredProcedure();
                obj.ID = int.Parse(base.GetParamValue("@ID").ToString());

                return kq;
			}
			catch
			{
				return false;
			}
		}

		public bool Update(SanPhamDTO obj)
		{
			try
			{
				base.sql = "usp_SanPham_Update";
				base.InitializeCommand();
				base.AddParameter("@ID", SqlDbType.Int, 1000, obj.ID);
				base.AddParameter("@IDDanhMucCon", SqlDbType.Int, 1000, obj.IDDanhMucCon);
				base.AddParameter("@IDToChucCaNhan", SqlDbType.Int, 1000, obj.IDToChucCaNhan);
				base.AddParameter("@TenSanPham", SqlDbType.NVarChar, 1000, obj.TenSanPham);
				base.AddParameter("@XuatXu", SqlDbType.NVarChar, 1000, obj.XuatXu);
				base.AddParameter("@MoTa", SqlDbType.NVarChar, 1000, obj.MoTa);
				base.AddParameter("@HinhAnh", SqlDbType.NVarChar, 1000, obj.HinhAnh);
				base.AddParameter("@SoLuongTon", SqlDbType.Int, 1000, obj.SoLuongTon);
				return ExecuteStoredProcedure();
			}
			catch
			{
				return false;
			}
		}

		public bool Delete(int id)
		{
			try
			{
				base.sql = "usp_SanPham_Delete";
				base.InitializeCommand();
				base.AddParameter("@ID", SqlDbType.Int, 1000, id);
				return ExecuteStoredProcedure();
			}
			catch
			{
				return false;
			}
		}

		public DataSet SelectByIDDanhMucCon(int iddanhmuccon)
		{
			DataSet ds;
			try
			{
				ds = new DataSet();
				base.sql = "usp_SanPham_SelectByIDDanhMucCon";
				base.InitializeCommand();
				base.AddParameter("@IDDanhMucCon", SqlDbType.Int, 1000, iddanhmuccon);
				base.FillDataSet(ref ds, "SanPham");
				return ds;
			}
			catch(System.Exception ex)
			{
				if (ex.InnerException != null)
				{
					CommonLogging.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					CommonLogging.Logger.Error(ex.Message);
				}
			}
			finally
			{
				base.CleanUpObject();
			}
			return null;
		}

		public DataSet SelectByIDToChucCaNhan(int idtochuccanhan)
		{
			DataSet ds;
			try
			{
				ds = new DataSet();
				base.sql = "usp_SanPham_SelectByIDToChucCaNhan";
				base.InitializeCommand();
				base.AddParameter("@IDToChucCaNhan", SqlDbType.Int, 1000, idtochuccanhan);
				base.FillDataSet(ref ds, "SanPham");
				return ds;
			}
			catch(System.Exception ex)
			{
				if (ex.InnerException != null)
				{
					CommonLogging.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					CommonLogging.Logger.Error(ex.Message);
				}
			}
			finally
			{
				base.CleanUpObject();
			}
			return null;
		}

	}
}